package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"rongyy/src/models"
	"rongyy/src/server"
	"rongyy/src/service"
)

type MerchantController struct {
	*service.MerchantService
}

func NewMerchantController() *MerchantController {
	return &MerchantController{}
}

func (u *MerchantController) Test(ctx *gin.Context) string {
	return "bosuai"
}

func (u *MerchantController) createMerchant(ctx *gin.Context) server.IModel {
	req := &models.MerchantModel{}
	err := ctx.ShouldBindJSON(req)
	token, err := u.Create(req)
	if err != nil {
		log.Println("decode: ", err)
	}
	return &models.DataModel{Msg: "新建成功", Data: token, IsSuccess: true}
}

func (u *MerchantController) GetByUnionid(c *gin.Context) server.IModel {
	json := &models.ReqUnionid{}
	err := c.ShouldBindJSON(json)
	if err != nil {
		log.Println("decode: ", err)
	}
	if len(json.Unionid) < 10 {
		return &models.DataModel{IsSuccess: false, Msg: "无效参数"}
	}
	user, has, err := u.FindByUnionid(json.Unionid)
	fmt.Println(user)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	if !has {
		return &models.DataModel{Msg: "查无此人"}
	}
	return &models.DataModel{Data: user}
}

func (u *MerchantController) wxLogin(c *gin.Context) server.IModel {
	req := &models.ReqWxLogin{}
	if err := c.ShouldBindJSON(req); err != nil {
		//middlewares.Logger.Println("ERROR - wxLogin bind json failed ", err)
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	data, err := u.WxLogin(req)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	return &models.DataModel{IsSuccess: true, Data: data}
}

func (us *MerchantController) profile(c *gin.Context) server.IModel {
	req := &models.UserModel{}
	if err := c.ShouldBindJSON(req); err != nil {
		//middlewares.Logger.Println("ERROR - wxLogin bind json failed ", err)
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	if len(req.Unionid) < 10 {
		return &models.DataModel{IsSuccess: false, Msg: "无效参数"}
	}
	wx, has, err := us.UserWxInfo(req)
	if !has {
		return &models.DataModel{IsSuccess: false, Msg: "查无此人"}
	}
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	data, _, err := us.Profile(req)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	return &models.DataModel{IsSuccess: true, Data: map[string]interface{}{
		"list": data,
		"wx":   wx,
	}}
}

func (u *MerchantController) RouterHandler(server *server.Server) {
	//server.Handle(http.MethodGet, "/sync", u.syncUnionId)
	//server.Handle(http.MethodGet, "/test", u.test)
	server.Handle(http.MethodPost, "/create", u.createMerchant)
	server.Handle(http.MethodPost, "/getByUnionid", u.GetByUnionid)
	//server.Handle(http.MethodPost, "/getById", u.GetById)
	//server.Handle(http.MethodPost, "/updateMpOpenid", u.UpdateMpOpenid)
	//server.Handle(http.MethodPost, "/getUserCardsByUnionid", u.GetUserCardsByUnionid)
	//server.Handle(http.MethodPost, "/getAllUsers", u.GetAllUsers)
	//server.Handle(http.MethodPost, "/updateUnionidById", u.UpdateUnionidById)
	//server.Handle(http.MethodGet, "/syncAllWxUser", u.syncAllWxUser)
	//server.Handle(http.MethodGet, "/syncAllOpenid", u.syncAllOpenid)
	server.Handle(http.MethodPost, "/wxLogin", u.wxLogin)
	server.Handle(http.MethodPost, "/profile", u.profile)
}
